package com.changba.schedule;

import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.changba.schedule.thread.VisibleThreadPool;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@SpringBootApplication
@MapperScan(basePackages = {"com.changba.schedule.mapper"})
@ComponentScan(basePackages = {"com.changba.schedule","com.chongba.cache"})
@EnableScheduling
public class ScheduleApplication {
    public static void main(String[] args) {
        SpringApplication.run(ScheduleApplication.class, args);
    }

    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }


    /**
     * 自定义线程池创建方式
     */
    @Bean("visibleThreadPool")
    public ThreadPoolTaskExecutor visibleThreadPool(){
        ThreadPoolTaskExecutor visibleThreadPool = new VisibleThreadPool();
        visibleThreadPool.setThreadNamePrefix("visibleThreadPool-");
        visibleThreadPool.setCorePoolSize(10);
        visibleThreadPool.setMaxPoolSize(1000);
        visibleThreadPool.setKeepAliveSeconds(60);
        visibleThreadPool.setQueueCapacity(1000);
        visibleThreadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        visibleThreadPool.initialize();
        return visibleThreadPool;
    }
}
